const express = require('express');
const router = express.Router();
const example = {
    "image1": "/20230622/GF1_PMS2_E106.4_N36.3_20230622_L5A3_0007355617.tif",
    "image2": "/20230622/GF1_PMS2_E106.4_N36.3_20171124_L1A0002793078.tif",
    "methodType": "1",
}
router.post('/imageAI', (req, res) => {

    // 从 req.body 中获取验证后的参数
    const { image1, image2, methodType, result } = req.body;
    if (!image1) return res.status(400).json({ errors: "影像文件1(image1)参数不能为空,示例如下", example });

    if (!methodType) {
        return res.status(400).json({ errors: "方法类型(methodType)参数不能为空,示例如下", example });
    } else {
        if (methodType < 1 || methodType > 4) return res.status(400).json({ errors: "方法类型(methodType)参数应为1-4,示例如下", example });
        if (methodType > 2 && !image2) return res.status(400).json({ errors: "影像文件2(image2)参数不能为空,示例如下", example });

        const filenameWithoutExtension = image1.replace(/\.[^.]+$/, '');

        res.json({
            message: '参数验证成功,返回结果如下：',
            params: {
                image1,
                image2,
                methodType
            },
            data: {
                result: filenameWithoutExtension + `_${methodType}.shp`
            },
        });
    }

});

module.exports = router;